package com.luntan.school.web.servlet.admin;

import com.luntan.school.web.listener.SessionListener;
import com.luntan.school.web.servlet.util.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "OnlineServlet", value = "/admin/online")
public class OnlineServlet extends BaseServlet {
    public void list() throws ServletException, IOException {
        request.setAttribute("onlineUserMap", SessionListener.onlineUserMap);
        request.getRequestDispatcher("/admin/online.jsp").forward(request, response);
    }

    public void offline() throws IOException {

        String sessionId = request.getParameter("sid"); //获取会话Id

        for(HttpSession session: SessionListener.onlineUserMap.keySet()){
            if(session.getId().equals(sessionId)) {
                session.removeAttribute("user"); // 删除session中的user对象(强退）
                SessionListener.onlineUserMap.remove(session); // 在线会员Map中删除该session
                break;
            }
        }

        response.sendRedirect(request.getContextPath() + "/admin/online?method=list");
    }
}
